package com.adcc.eoss.cawebservice.domain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.adcc.eoss.cawebservice.StringUtil;

@Repository
public class PilotRepository {

	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	protected JdbcTemplate jdbc;

	@Transactional(readOnly = true)
	public synchronized List<Pilot> getPilot(String id_card) {
		try {
			List<Pilot> pilots = new ArrayList<Pilot>();
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT id,code,name,deptid,id_card,gender,subteam,own,cockpit,valt,phr"
					+ " FROM dd_da_archive_smp WHERE id_card = '" + id_card + "'");
			List<Map<String, Object>> list = jdbc.queryForList(sql.toString());
			Iterator<Map<String, Object>> iterator = list.iterator();
			Pilot pilot = null;
			while (iterator.hasNext()) {
				Map<String, Object> mapPilot = (Map<String, Object>) iterator.next();
				pilot = new Pilot();
				pilot.setId((BigDecimal) mapPilot.get("id"));
				pilot.setCockpit((Integer) mapPilot.get("cockpit"));
				pilot.setCode((String) mapPilot.get("code"));
				pilot.setDeptid((String) mapPilot.get("deptid"));
				pilot.setGender((Integer) mapPilot.get("gender"));
				pilot.setId_card((String) mapPilot.get("id_card"));
				pilot.setName((String) mapPilot.get("name"));
				pilot.setOwn((String) mapPilot.get("own"));
				pilot.setPhr((String) mapPilot.get("phr"));
				pilot.setSubteam((String) mapPilot.get("subteam"));
				pilot.setValt(StringUtil.DateToString((Date) mapPilot.get("valt")));
				pilots.add(pilot);
			}
			return pilots;
		} catch (Exception ex) {
			log.error(ex.getMessage());
			return null;
		}
	}

	@Transactional(readOnly = true)
	public synchronized List<Pilot> getPilotByName(String name) {
		try {
			List<Pilot> pilots = new ArrayList<Pilot>();
			StringBuilder sql = new StringBuilder();
			sql.append("SELECT id,code,name,deptid,id_card,gender,subteam,own,cockpit,valt,phr"
					+ " FROM dd_da_archive_smp WHERE name = '" + name + "'");
			List<Map<String, Object>> list = jdbc.queryForList(sql.toString());
			Iterator<Map<String, Object>> iterator = list.iterator();
			Pilot pilot = null;
			while (iterator.hasNext()) {
				Map<String, Object> mapPilot = (Map<String, Object>) iterator.next();
				pilot = new Pilot();
				pilot.setId((BigDecimal) mapPilot.get("id"));
				pilot.setCockpit((Integer) mapPilot.get("cockpit"));
				pilot.setCode((String) mapPilot.get("code"));
				pilot.setDeptid((String) mapPilot.get("deptid"));
				pilot.setGender((Integer) mapPilot.get("gender"));
				pilot.setId_card((String) mapPilot.get("id_card"));
				pilot.setName((String) mapPilot.get("name"));
				pilot.setOwn((String) mapPilot.get("own"));
				pilot.setPhr((String) mapPilot.get("phr"));
				pilot.setSubteam((String) mapPilot.get("subteam"));
				pilot.setValt(StringUtil.DateToString((Date) mapPilot.get("valt")));
				pilots.add(pilot);
			}
			return pilots;
		} catch (Exception ex) {
			log.error(ex.getMessage());
			return null;
		}
	}

}